#! /usr/bin/python
# -*- coding:utf-8 -*-

## ========================================================================
## Copyright (C) 2007-2013  Jean-Philippe GOLAY
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
## ========================================================================

from persistance import *

class Product:
  
  def __init__(self):
    self.mDB=ManageDB(Glob.dbName, Glob.user, Glob.passwd, Glob.host)
    logging.basicConfig(filename=Glob.LOG_FILENAME,level=logging.DEBUG)


  def htmlList(self):
    if (self.mDB.executSQL(Glob.select_all_from_product) == 0):
      logging.debug(u'Product: htmlList() SQL error : %s' % Glob.select_all_from_product)
      return u"<br/><br/><b>Impossible de r&eacute;pondre &agrave; cette demande...</b><br/>"
    else:
      result=self.mDB.resultSQL()
      if len(result) > 0:
        html_list = "<br/><h3>Liste des produits&nbsp;&nbsp;<a href='#' onclick='newProduct()' ><img src='img/add_icon.gif' title='Nouveau produit' width='16' height='16' ></a></h3><br/><table border='0' cellspacing='0' cellpadding='0' align='center' width='100%' class='products_list'>\n<tr class='headerList'>\n<td>D&eacute;signation</td>\n<td colspan='2'>Actions</td>\n</tr>\n"
        for row in result:
          html_list += "<tr>\n<td>%s</td>\n<td><a href'#' id='%s' onclick='modifyProduct(this.id)'><img src='img/edit.gif' width='16' height='16' title='Modifier'></a></td>\n<td><a href'#' id='%s' onclick='deleteProduct(this.id)'><img src='img/delete_icon.gif' width='17' height='16' title='Supprimer'></a></td>\n</tr>" % (row[2], row[0], row[0])
        html_list += "</table><br/>\n"
        return html_list
      else:
        return u"<br/><h3>Liste des produits&nbsp;&nbsp;<a href='#' onclick='newProduct()' ><img src='img/add_icon.gif' title='Nouveau produit' width='16' height='16' ></a></h3><br/><br/><b>Aucun Produits enregistr&eacute; !</b><br/>"


  def productList(self):
    if (self.mDB.executSQL(Glob.select_all_from_product) == 0):
      logging.debug(u'Product: productList() SQL error : %s' % Glob.select_all_from_product)
      return 0
    else:
      result=self.mDB.resultSQL()
      if len(result) > 0:
        return result
      else:
        return 0


  def fieldName(self):
    return { u'id': 0, u'prod_ref': 1, u'prod_desig': 2, u'prod_desc': 3, u'prod_option': 4, u'prod_img': 5, u'prod_price': 6, u'prod_tax': 7, u'prod_dispo': 8, u'prod_stock': 9 }

  
  # 0:`id` 1:`prod_ref` 2:`prod_desig` 3:`prod_desc` 4:`prod_option` 5:`prod_img`
  # 6:`prod_price` 7:`prod_tax` 8:`prod_dispo` 9:`prod_stock`
  def read(self, id):
    if (self.mDB.executSQL(Glob.select_all_from_product_where_id % id) == 0):
      sql=Glob.select_all_from_product_where_id % id
      logging.debug(u'Product: read() SQL error : %s' % sql)
      return 0
    else:
      result=self.mDB.resultSQL()
      if len(result) > 0:
        product = {}
        for row in result:
          product[u'id']=row[0]
          product[u'prod_ref']=row[1]
          product[u'prod_desig']=row[2]
          product[u'prod_desc']=row[3]
          product[u'prod_option']=row[4]
          product[u'prod_img']=row[5]
          product[u'prod_price']=row[6]
          product[u'prod_tax']=row[7]
          product[u'prod_dispo']=row[8]
          product[u'prod_stock']=row[9]
        if product.has_key(u'id'):
          return product
        else:
          return 0
      else:
        return 0


  def update(self, id, prod_ref, prod_desig, prod_desc, prod_option, prod_img, prod_price, prod_tax, prod_dispo, prod_stock):
    u_prod_ref=unicode(prod_ref.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_desig=unicode(prod_desig.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_desc=unicode(prod_desc.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_option=unicode(prod_option.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_img=unicode(prod_img.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_price=unicode(prod_price, u'utf8')
    u_prod_tax=unicode(prod_tax, u'utf8')
    u_prod_dispo=unicode(prod_dispo, u'utf8')
    u_prod_stock=unicode(prod_stock, u'utf8')
    if (self.mDB.executSQL(Glob.update_product_where_id % (u_prod_ref, u_prod_desig, u_prod_desc, u_prod_option, u_prod_img, u_prod_price, u_prod_tax, u_prod_dispo, u_prod_stock, id) ) == 0):
      sql=Glob.update_product_where_id % (u_prod_ref, u_prod_desig, u_prod_desc, u_prod_option, u_prod_img, u_prod_price, u_prod_tax, u_prod_dispo, u_prod_stock, id)
      logging.debug(u'Product: update() SQL error : %s' % sql)
      return 0
    else:
      return 1    


  def create(self, prod_ref, prod_desig, prod_desc, prod_option, prod_img, prod_price, prod_tax, prod_dispo, prod_stock):
    u_prod_ref=unicode(prod_ref.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_desig=unicode(prod_desig.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_desc=unicode(prod_desc.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_option=unicode(prod_option.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_img=unicode(prod_img.replace('"', '\\"').replace("'", "\\'"), u'utf8')
    u_prod_price=unicode(prod_price, u'utf8')
    u_prod_tax=unicode(prod_tax, u'utf8')
    u_prod_dispo=unicode(prod_dispo, u'utf8')
    u_prod_stock=unicode(prod_stock, u'utf8')
    if (self.mDB.executSQL(Glob.insert_product % (u_prod_ref, u_prod_desig, u_prod_desc, u_prod_option, u_prod_img, u_prod_price, u_prod_tax, u_prod_dispo, u_prod_stock) ) == 0):
      sql=Glob.insert_product % (u_prod_ref, u_prod_desig, u_prod_desc, u_prod_option, u_prod_img, u_prod_price, u_prod_tax, u_prod_dispo, u_prod_stock)
      logging.debug(u'Product: create() SQL error : %s' % sql)
      return 0
    else:
      if (self.mDB.executSQL(Glob.last_insert_id_product) == 0):
        logging.debug(u'Product: create() SQL error : %s' % Glob.last_insert_id_product)
        return 0
      else:
        result=self.mDB.resultSQL()
        for row in result:
          id=row[0]
        if id:
          return id 


  def delete(self, id):
    if (self.mDB.executSQL(Glob.delete_product_where_id % id) == 0):
      sql=Glob.delete_product_where_id % id
      logging.debug(u'Product: delete() SQL error : %s' % sql)
      return 0
    else:
      return 1
